package com.jinyou.chengfengshop.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jinyou.chengfengshop.Constant.AppUrl;
import com.jinyou.chengfengshop.OkHttpUtils.CallBackUtil;
import com.jinyou.chengfengshop.OkHttpUtils.OkhttpUtil;
import com.jinyou.chengfengshop.R;
import com.jinyou.chengfengshop.adapter.RechargeListAdapter;
import com.jinyou.chengfengshop.bean.CostBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * 商户获取自己的消费记录
 * Created by zhuzhiqiang on 2018/9/26.
 */

public class RechargeListActivity extends AppCompatActivity {
    private TextView back_tv;
    private TextView recharge_tv;
    private RecyclerView recharge_rec;
    private RechargeListAdapter mAdapter;
    private List<CostBean> rgb = new ArrayList<CostBean>();
    private TextView wallet_tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_recharge);

        back_tv = (TextView) findViewById(R.id.back_tv);
        back_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recharge_tv = (TextView) findViewById(R.id.recharge_tv);
        recharge_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(getApplicationContext(),RechargeActivity.class);
                startActivity(mIntent);
            }
        });

        recharge_rec = (RecyclerView) findViewById(R.id.recharge_rec);

        GridLayoutManager layoutManage = new GridLayoutManager(this, 1);
        recharge_rec.setLayoutManager(layoutManage);
        wallet_tv = (TextView) findViewById(R.id.wallet_tv);

    }




    //商家消费
    private void shopWallet(){
        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        String tocken = sh.getString("tocken","");
        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("token",tocken);
        paramsMap.put("sysCustomer","o2o-chengfeng");

        OkhttpUtil.okHttpPost(AppUrl.SHOP_WALLET, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(RechargeListActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {
                    Log.i("shopCost",response);
                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");

                    if(code == 1){
                        JSONObject ob = obj.getJSONObject("info");
                        //String ob = obj.getString("data");
                        wallet_tv.setText(ob.getString("balance"));

                    }else {
                        if(obj.getString("type").equals("notLogin")){
                            reLogin();
                        }
                        Toast.makeText(RechargeListActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }



    //商家消费
    private void shopCost(){
        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        String tocken = sh.getString("tocken","");
        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("token",tocken);
        paramsMap.put("sysCustomer","o2o-chengfeng");

        OkhttpUtil.okHttpPost(AppUrl.SHOP_COST, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(RechargeListActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {
                    Log.i("shopCost",response);
                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");

                    if(code == 1){
                        rgb.clear();
                        String ob = obj.getString("data");
                        //wallet_tv.setText(obj.getString("wallet"));
                        if (ob != null && !ob.equals("")) {
                            Type typeOfT = new TypeToken<List<CostBean>>() {
                            }.getType();
                            rgb = new Gson().fromJson(ob, typeOfT);
                        }


                        mAdapter = new RechargeListAdapter(getApplicationContext(),rgb);
                        recharge_rec.setAdapter(mAdapter);



                    }else {
                        if(obj.getString("type").equals("notLogin")){
                            reLogin();
                        }
                        Toast.makeText(RechargeListActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }


    //重新登陆
    private void reLogin(){
        SharedPreferences sh = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor edt = sh.edit();
        edt.putString("tocken","");
        edt.putString("info","");
        edt.commit();
        Intent mIntent = new Intent(RechargeListActivity.this,LoginActivity.class);
        startActivity(mIntent);
        finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
        shopCost();
        shopWallet();
    }
}
